<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<button id="btn">复制</button>
<table id="table">
    <thead>
    <tr>
        <th>id</th>
        <th>name</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>aaa</td>
        <td>aaa</td>
    </tr>
    <tr>
        <td>bbb</td>
        <td>bbb</td>
    </tr>
    </tbody>

</table>

<script>
    document.getElementById('btn').onclick = function () {
        var table = document.getElementById('table');
        var range = document.createRange();
        range.selectNode(table);
        var selection = window.getSelection();
        if (selection.rangeCount > 0) selection.removeAllRanges();
        selection.addRange(range);
        document.execCommand('copy');
        alert('复制成功');
    }
</script>
</body>
</html>